//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view.components
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.List;
	import mx.core.mx_internal;
	import mx.events.ListEvent;
	import mx.events.ListEventReason;
	
	use namespace mx_internal;

	public class BlogItemList extends List
	{
		public function BlogItemList()
		{
			super();			
		}
		
		override public function set dataProvider(value:Object):void
	    {	       
	        super.dataProvider = value;
	        
	        if(value)
	        {
	        	var sort:Sort = new Sort();
				var dp:ArrayCollection = this.dataProvider as ArrayCollection;
				
				sort.fields = [new SortField("time", false, value)]
				dp.sort = sort;
				dp.refresh();
	        }
	        
	    }
	    
		public function setSelectedIndex(scrollDirection:int):void
	    {
	    	if(!dataProvider) return;
	    	var l:int = ArrayCollection(dataProvider).source.length;
	    	if(l == 0) return;
	    	if(scrollDirection > 0 && selectedIndex >= (l - 1)) 
	    	{return;}
	    	if(scrollDirection < 0 && selectedIndex <= 0) return;
	    	 	
	    	if(this.verticalScrollBar && this.verticalScrollBar.visible)
	    		this.verticalScrollBar.mx_internal::lineScroll(scrollDirection);
            
            if(this.selectedIndex == -1)
            	 this.selectedIndex = 0;
            else	 
            	this.selectedIndex += scrollDirection;
            
            dispatchEvent(new ListEvent(ListEvent.CHANGE));
	    }
	    
	    override protected function mouseWheelHandler(event:MouseEvent):void
	    {
	    	if (itemEditorInstance)
            endEdit(ListEventReason.OTHER);
            if (verticalScrollBar && verticalScrollBar.visible)
	        {
	            event.stopPropagation();
	            if(event.delta > 0)
	            {
	            	setSelectedIndex(-1)
	            }
	            else if(event.delta < 0)
	            {
	            	setSelectedIndex(1)
	            }
	        }
	    }

	}
}